<template>
  <div class="aded">
    <el-dialog :title="form.id ? '编辑权限' : '新增权限'" :visible="showAded">
      <el-form ref="myform" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="企业可见">
          <!-- <el-input v-model="form.enVisible" /> -->
          <el-switch
            v-model="form.enVisible"
            style="display: block"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
            active-text="可见"
            inactive-text="不可见"
            class="myswitch"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSysPermissionApi,
  setSysPermissionApi,
  addSysPermissionApi
} from '@/api/permission'
export default {
  props: {
    showAded: Boolean
  },
  data() {
    return {
      form: {
        enVisible: '', //	是否可见
        name: '', //	权限名称
        code: '', //	权限标识
        description: '', //	权限描述
        type: '', //	区分页面权限 & 按钮权限
        pid: '' //	是否存在父权限
      },
      rules: {
        name: [{ required: true, message: '不合法', trigger: 'blur' }],
        code: [{ required: true, message: '不合法', trigger: 'blur' }],
        description: [{ required: true, message: '不合法', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getSysPermissionApi(id) {
      this.form = await getSysPermissionApi(id)
      //   console.log(this.form)
    },
    cancel() {
      this.$emit('update:showAded', false)
      this.form = {
        enVisible: '', //	是否可见
        name: '', //	权限名称
        code: '', //	权限标识
        description: '', //	权限描述
        type: '', //	区分页面权限 & 按钮权限
        pid: '' //	是否存在父权限
      }
    },
    setProp(type, pid) {
      this.form.type = type
      this.form.pid = pid
    },
    submit() {
      this.$refs.myform.validate(async res => {
        if (res) {
          if (this.form.id) {
            // console.log(1)
            // console.log(this.form.id)
            // const res1 = await setSysPermissionApi(this.form)

            // console.log(res1)
            await setSysPermissionApi(this.form)
          } else {
            await addSysPermissionApi(this.form)
          }
          this.$message.success('操作成功')
          this.$emit('update:showAded', false)
          this.$emit('updateAded')
          this.form = {
            enVisible: '', //	是否可见
            name: '', //	权限名称
            code: '', //	权限标识
            description: '', //	权限描述
            type: '', //	区分页面权限 & 按钮权限
            pid: '' //	是否存在父权限
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.aded {
  .myswitch {
    margin-top: 9px;
  }
}
</style>
